import express from 'express'
import { getRecords } from './db'
import { authMiddleware } from './auth.middleware'

export const recordRoute = express.Router()

/**
 * @swagger
 * /records:
 *   post:
 *     summary: 获取用户账单记录
 *     description: 根据用户ID和日期获取账单记录列表
 *     tags: [Records]
 *     security:
 *       - bearerAuth: []
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             $ref: '#/components/schemas/RecordRequest'
 *     responses:
 *       200:
 *         description: 成功获取记录列表
 *         content:
 *           application/json:
 *             schema:
 *               type: array
 *               items:
 *                 type: object
 *                 properties:
 *                   id:
 *                     type: string
 *                   amount:
 *                     type: number
 *                   title:
 *                     type: string
 *                   date:
 *                     type: string
 *                     format: date
 *                   user_id:
 *                     type: string
 *       401:
 *         description: 未授权访问
 *         content:
 *           application/json:
 *             schema:
 *               $ref: '#/components/schemas/Error'
 *       500:
 *         description: 服务器内部错误
 *         content:
 *           application/json:
 *             schema:
 *               $ref: '#/components/schemas/Error'
 */
recordRoute.post('/', authMiddleware, async (req, res) => {
  const { user_id, date } = req.body
  // date 格式 YYYY-MM-DD
  try {

    const records = await getRecords(user_id, date)
    console.log('进入record接口...') 
    res.status(200).json(records)

  } catch (error) {
    console.log(error)
    res.status(500).json({
      error: 'Internal Server Error'
    })
  }
  
  
})